<?php

//连接数据库
$dsn = 'mysql:host=localhost;dbname=hw;port=3306;charset=utf8';
$user = 'root';
$pass = '';
try{
    $conn = new pdo($dsn,$user,$pass);
}catch (exception $e){
    exit($e);
//关闭数据库
function close(){
    global $conn;
    unset($conn);
}





	
	

//增
}
function save($tn,$arr_assoc){
    global $conn;
    foreach($arr_assoc as $key=>$value){
        $filds[] = $key;
        $values[] = ':' . $key;
    }
    $filds = implode(',',$filds);
    $values = implode(',',$values);
    $stmt = $conn->prepare("insert into $tn($filds) values($values)");
    $stmt->execute($arr_assoc);//参数绑定再看看
    $stmt->closeCursor();
}



//删 跟主键ID
function deleteById($tn,$id){
    global $conn;
    $pk = getPK($tn);
    $stmt = $conn->prepare("delete from $tn where $pk=?");
    $stmt->execute(array($id));
    $stmt->closeCursor();
}
//删 跟条件 包含deleteById 只是 参数简写 省略了主键字段名 ($tn,23) ($tn,stu_id=23) ($tn,stu_name='jack')
function deleteByWhere($tn,$w='1=1'){
    global $db;
    $stmt = $db->prepare("delete from $tn where $w");
    $stmt->execute();
    $stmt->closeCursor();
}

//改
function updateByPID($tn,$arr_assoc){
    global $conn;
    $pk = getpk($tn);
    if(empty($pk)) return;
    if(!array_key_exists($pk,$arr_assoc)) return;//参数的关联数组中没有主键字段？
    $id = $arr_assoc[$pk];
    foreach($arr_assoc as $key=>$value){
        if($key==$pk) continue;//参数的关联数组中去掉主键字段
        $filds[] = $key.'=:'.$key;
    }
    $filds = implode(',',$filds);
    $stmt = $conn->prepare("update $tn set $filds where $pk=:$pk");
    $stmt->execute($arr_assoc);
    $stmt->closeCursor();
}

//
function updateByWhere($tn,$arr_assoc,$w){
    global $conn;
    foreach($arr_assoc as $key=>$value){
        $filds[] = $key.'=:'.$key;
    }
    $filds = implode(',',$filds);
    $stmt = $conn->prepare("update $tn set $filds where $w");
    $stmt->execute($arr_assoc);
    $stmt->closeCursor();
}

//
function updateinc($tn,$f,$n=1,$w='1=1'){
    global $db;
    $stmt = $db->prepare("update $tn set $f=$f+$n where $w");
    $stmt->execute();
    $stmt->closeCursor();
}

//查 跟主键ID 默认查所有字段
function queryById($tn,$id,$f='*'){
    global $conn;
    $pk = getpk($tn);
    $stmt = $conn->prepare("select $f from $tn where $pk=?");
    $stmt->execute(array($id));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    return array_shift($rows);
}

//查 跟 $f字段 $w条件 $o排序呢 $l分页
function query($tn,$f='*',$w='1=1',$o='',$l=''){
    global $conn;
    $stmt = $conn->prepare("select $f from $tn where $w $o $l");
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    return $rows;
}








//分页
function page($tn,$ps=10,$f='*',$w='1=1',$o='',$tp=''){
    global $db;
    $stmt = $db->prepare("select count(*) from $tn where $w");
    $stmt->execute();
    $stmt->bindColumn(1,$recordcount);
    $stmt->fetch(PDO::FETCH_BOUND);
    $stmt->closeCursor();
    $pagecount = ceil($recordcount/$ps);
    $p = isset($_GET['p']) ? $_GET['p'] : 1;
    if($p<1) $p=1;
    if($p>$pagecount) $p = $pagecount;
    $dd['recordcount'] = $recordcount;
    $dd['pagecount'] = $pagecount;
    $dd['p'] = $p;

    //求这一页的数据
    $stmt = $db->prepare("select $f from $tn where $w $o limit ?,?");
    $start = $p*$ps-$ps;
    $stmt->bindParam(1,$start,PDO::PARAM_INT);
    $stmt->bindParam(2,$ps,PDO::PARAM_INT);
    $stmt->execute();
    $dd['rows'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $stmt->closeCursor();

    //实现百度分页效果
    $info = '<div class="page">';
    $ss = 1;
    $ee = 10;
    if($p>6){
        $ss = $p-5;
        $ee = $p+4;
    }
    if($ss>1) $info.=sprintf('<a href="?p=%d">首页</a>',1);
    if($p>1) $info.=sprintf('<a href="?p=%d">上一页</a>',$p-1);
    for($i=$ss;$i<=$ee;$i++){
        if($i>$pagecount) break;
        if($i==$p){
            $info.=sprintf('<span>%d</span>',$i);
            continue;
        }
        $info.=sprintf('<a href="?p=%d">%d</a>',$i,$i);
    }
    if($p!=$pagecount) $info.=sprintf('<a href="?p=%d">下一页</a>',$p+1);
    if($p<$pagecount)  $info.=sprintf('<a href="?p=%d">尾页</a>',$pagecount);
    $info.='</div>';
    $dd['info'] = $info;
    return $dd;
}




//获取主键
function getPK($tn) {
    global $conn;
    $stmt = $conn->prepare("show full columns from $tn");
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $pk = null;
    foreach ($rows as $value) {
        if ($value['Key'] == 'PRI') {
            $pk = $value['Field'];
            break;
        }
    }
    return $pk;
}

//表中是否存在 $w条件 (name='jack')
function exists($tn,$w){
    global $conn;
    $flag = false;
    if(empty($w)) return $flag;
    $stmt = $conn->prepare("select count(*) from $tn where $w");
    $stmt->execute();
    $stmt->bindColumn(1,$c);
    $stmt->fetch(PDO::FETCH_BOUND);
    $stmt->closeCursor();
    if($c>=1) $flag = true;
    return $flag;
}

function mm($pass,$account){
    $mm = $pass.$account;
    $m1 = md5($mm);
    $m2 = sha1($mm);
    $mm = substr($m1,0,5).substr($m2,0,5);
    $mm .= substr($m1,10,5).substr($m2,10,5);
    $mm .= substr($m1,20,5).substr($m2,20,7);
    return $mm;
}





